package net.caixiaomi.info.ui.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseTransparentDialogFragment;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.RechargeReceiveBonus;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.ui.coupons.CouponsActivity;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

public class RedPocketDialog extends BaseTransparentDialogFragment {
    @BindView(R.id.btn_next)
    View mBtnNext;
    @BindView(R.id.container)
    View mContainer;
    @BindView(R.id.action)
    TextView mAction;
    @BindView(R.id.price)
    TextView mPrice;
    private RechargeReceiveBonus mModel;

    /**
     * 领取
     */
    @OnClick(R.id.btn_next)
    void toNext() {
        if (mModel != null) {
            getDialog().setCanceledOnTouchOutside(true);
            getDialog().setCancelable(true);
            mBtnNext.setVisibility(View.GONE);
            mContainer.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.btn_close)
    void toClose() {
        dismissAllowingStateLoss();
    }

    /**
     * 去看看
     */
    @OnClick(R.id.btn_look)
    void toLook() {
        Intent intent = new Intent(getActivity(), CouponsActivity.class);
        startActivity(intent);
        toClose();
    }

    @Override
    protected int layoutRes() {
        return R.layout.dialog_red_pocket;
    }

    @Override
    protected void onViewCreated(@NonNull View view) {

        Bundle arguments = getArguments();
        String payLogId = arguments.getString("payLogId");
        boolean isNewUser = arguments.getBoolean("isNewUser");

        getDialog().setCanceledOnTouchOutside(!isNewUser ? false : true);
        getDialog().setCancelable(!isNewUser ? false : true);

        mBtnNext.setVisibility(!isNewUser ? View.VISIBLE : View.GONE);
        mContainer.setVisibility(isNewUser ? View.VISIBLE : View.GONE);

        loadData(payLogId);

    }

    private void loadData(String payLogId) {
        JSONObject params = new JSONObject();
        params.put("payLogId", payLogId);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<RechargeReceiveBonus>> rechargeSucReiceiveBonus = requestService.rechargeSucReiceiveBonus(params);
        rechargeSucReiceiveBonus.enqueue(new ResponseCallback<BaseCallModel<RechargeReceiveBonus>>(getActivity()) {
            @Override
            public void onSuccess(BaseCallModel<RechargeReceiveBonus> data) {
                mModel = data.data;
                mPrice.setText(getString(R.string.C_RECHARGE_GIVE_BOUND, mModel.getDonationPrice()));
            }

            @Override
            public void onFail(ResponseError error) {
            }

            @Override
            public void onError() {
            }
        });

    }

}
